mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
proc.h
Go to the documentation of this file.
1
6
7#ifndef MRUBY_PROC_H
8#define MRUBY_PROC_H
9
10#include "common.h"
11#include <mruby/irep.h>
12#include <string.h>
13
18
19/*
20 * env object (for internal used)
21 *
22 * - don't create multiple envs on one ci.
23 * - don't share a env to different ci.
24 * - don't attach a closed env to any ci.
25 */
26struct REnv {
27 MRB_OBJECT_HEADER;
28 mrb_value *stack;
29 struct mrb_context *cxt; /* if not null, it means that the stack is shared with the call frame */
30 mrb_sym mid;
31};
32
33/* flags (20bits): 1(ZERO):1(separate module):2(visibility):8(cioff/bidx):8(stack_len) */
34#define MRB_ENV_SET_LEN(e,len) ((e)->flags = (((e)->flags & ~0xff)|((unsigned int)(len) & 0xff)))
35#define MRB_ENV_LEN(e) ((mrb_int)((e)->flags & 0xff))
36#define MRB_ENV_CLOSE(e) ((e)->cxt = NULL)
37#define MRB_ENV_ONSTACK_P(e) ((e)->cxt != NULL)
38#define MRB_ENV_BIDX(e) (((e)->flags >> 8) & 0xff)
39#define MRB_ENV_SET_BIDX(e,idx) ((e)->flags = (((e)->flags & ~(0xff<<8))|((unsigned int)(idx) & 0xff)<<8))
40#define MRB_ENV_SET_VISIBILITY(e, vis) MRB_FLAGS_SET((e)->flags, 16, 2, vis)
41#define MRB_ENV_VISIBILITY(e) MRB_FLAGS_GET((e)->flags, 16, 2)
42#define MRB_ENV_VISIBILITY_BREAK_P(e) MRB_FLAG_CHECK((e)->flags, 18)
43#define MRB_ENV_COPY_FLAGS_FROM_CI(e, ci) MRB_FLAGS_SET((e)->flags, 16, 3, (ci)->vis)
44
45/*
46 * Returns TRUE on success.
47 * If the function fails:
48 * * Returns FALSE if noraise is TRUE.
49 * * Raises a NoMemoryError exception if noraise is FALSE.
50 */
51mrb_bool mrb_env_unshare(mrb_state*, struct REnv*, mrb_bool noraise);
52
53struct RProc {
54 MRB_OBJECT_HEADER;
55 union {
56 const mrb_irep *irep;
57 mrb_func_t func;
58 mrb_sym mid;
59 } body;
60 const struct RProc *upper;
61 union {
62 struct RClass *target_class;
63 struct REnv *env;
64 } e;
65};
66
67/* aspec access */
68#define MRB_ASPEC_REQ(a) (((a) >> 18) & 0x1f)
69#define MRB_ASPEC_OPT(a) (((a) >> 13) & 0x1f)
70#define MRB_ASPEC_REST(a) (((a) >> 12) & 0x1)
71#define MRB_ASPEC_POST(a) (((a) >> 7) & 0x1f)
72#define MRB_ASPEC_KEY(a) (((a) >> 2) & 0x1f)
73#define MRB_ASPEC_KDICT(a) (((a) >> 1) & 0x1)
74#define MRB_ASPEC_BLOCK(a) ((a) & 1)
75#define MRB_ASPEC_NOBLOCK(a) (((a) >> 23) & 0x1)
76
77#define MRB_PROC_CFUNC_FL 128
78#define MRB_PROC_CFUNC_P(p) (((p)->flags & MRB_PROC_CFUNC_FL) != 0)
79#define MRB_PROC_CFUNC(p) (p)->body.func
80#define MRB_PROC_STRICT 256
81#define MRB_PROC_STRICT_P(p) (((p)->flags & MRB_PROC_STRICT) != 0)
82#define MRB_PROC_ORPHAN 512
83#define MRB_PROC_ORPHAN_P(p) (((p)->flags & MRB_PROC_ORPHAN) != 0)
84#define MRB_PROC_ENVSET 1024
85#define MRB_PROC_ENV_P(p) (((p)->flags & MRB_PROC_ENVSET) != 0)
86#define MRB_PROC_ENV(p) (MRB_PROC_ENV_P(p) ? (p)->e.env : NULL)
87#define MRB_PROC_TARGET_CLASS(p) (MRB_PROC_ENV_P(p) ? (p)->e.env->c : (p)->e.target_class)
88#define MRB_PROC_SET_TARGET_CLASS(p,tc) do {\
89 if (MRB_PROC_ENV_P(p)) {\
90 (p)->e.env->c = (tc);\
91 mrb_field_write_barrier(mrb, (struct RBasic*)(p)->e.env, (struct RBasic*)(tc));\
92 }\
93 else {\
94 (p)->e.target_class = (tc);\
95 mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)(tc));\
96 }\
97} while (0)
98#define MRB_PROC_SCOPE 2048
99#define MRB_PROC_SCOPE_P(p) (((p)->flags & MRB_PROC_SCOPE) != 0)
100#define MRB_PROC_NOARG 4096 /* for MRB_PROC_CFUNC_FL, aspec == MRB_ARGS_NONE() */
101#define MRB_PROC_NOARG_P(p) (((p)->flags & MRB_PROC_NOARG) != 0)
102#define MRB_PROC_ALIAS 8192
103#define MRB_PROC_ALIAS_P(p) (((p)->flags & MRB_PROC_ALIAS) != 0)
104
105/* Compressed aspec for cfunc procs (13 bits in RProc.flags).
106 * Uses free bits 0-6 and 14-19 to store a compressed argument spec.
107 * Layout: block(0) kdict(1) key(2-3) post(4-5) rest(6) opt(14-16) req(17-19)
108 * Field widths are smaller than the full 24-bit aspec: req/opt max 7, post/key max 3.
109 * Values exceeding the compressed range are clamped and rest is forced to 1. */
110#define MRB_PROC_CASPEC_MASK 0xfc07fu /* bits 0-6 and 14-19 */
111
112static inline uint32_t
113mrb_proc_compress_aspec(mrb_aspec aspec)
114{
115 uint32_t req = MRB_ASPEC_REQ(aspec);
116 uint32_t opt = MRB_ASPEC_OPT(aspec);
117 uint32_t rest = MRB_ASPEC_REST(aspec);
118 uint32_t post = MRB_ASPEC_POST(aspec);
119 uint32_t key = MRB_ASPEC_KEY(aspec);
120 uint32_t kdict = MRB_ASPEC_KDICT(aspec);
121 uint32_t block = MRB_ASPEC_BLOCK(aspec);
122
123 if (req > 7 || opt > 7 || post > 3 || key > 3) {
124 if (req > 7) req = 7;
125 if (opt > 7) opt = 7;
126 if (post > 3) post = 3;
127 if (key > 3) key = 3;
128 rest = 1;
129 }
130
131 return block | (kdict << 1) | (key << 2) | (post << 4) | (rest << 6)
132 | (opt << 14) | (req << 17);
133}
134
135static inline mrb_aspec
136mrb_proc_decompress_caspec(uint32_t flags)
137{
138 return (((flags >> 17) & 0x7) << 18) /* req */
139 | (((flags >> 14) & 0x7) << 13) /* opt */
140 | (((flags >> 6) & 0x1) << 12) /* rest */
141 | (((flags >> 4) & 0x3) << 7) /* post */
142 | (((flags >> 2) & 0x3) << 2) /* key */
143 | (((flags >> 1) & 0x1) << 1) /* kdict */
144 | (flags & 0x1); /* block */
145}
146
147static inline void
148mrb_proc_set_cfunc_aspec(struct RProc *p, mrb_aspec aspec)
149{
150 p->flags &= ~(MRB_PROC_NOARG | MRB_PROC_CASPEC_MASK);
151 if (aspec == 0) {
152 p->flags |= MRB_PROC_NOARG;
153 }
154 else {
155 p->flags |= mrb_proc_compress_aspec(aspec);
156 }
157}
158
159#define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v)))
160
161struct RProc *mrb_proc_new(mrb_state*, const mrb_irep*);
162MRB_API struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t);
163MRB_API struct RProc *mrb_closure_new_cfunc(mrb_state *mrb, mrb_func_t func, int nlocals);
164
165/* following functions are defined in mruby-proc-ext so please include it when using */
166MRB_API struct RProc *mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const mrb_value *argv);
167MRB_API mrb_value mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx);
168/* old name */
169#define mrb_cfunc_env_get(mrb, idx) mrb_proc_cfunc_env_get(mrb, idx)
170
171#define MRB_METHOD_FUNC_FL (1 << 24)
172#define MRB_METHOD_PUBLIC_FL 0
173#define MRB_METHOD_PRIVATE_FL (1 << 25)
174#define MRB_METHOD_PROTECTED_FL (1 << 26)
175#define MRB_METHOD_VDEFAULT_FL ((1 << 25) | (1 << 26))
176#define MRB_METHOD_VISIBILITY_MASK ((1 << 25) | (1 << 26))
177
178#define MRB_METHOD_FUNC_P(m) ((m).flags&MRB_METHOD_FUNC_FL)
179#define MRB_METHOD_FUNC(m) ((m).as.func)
180#define MRB_METHOD_FROM_FUNC(m,fn) do{(m).flags=MRB_METHOD_FUNC_FL;(m).as.func=(fn);}while(0)
181#define MRB_METHOD_FROM_PROC(m,pr) do{(m).flags=0;(m).as.proc=(pr);}while(0)
182#define MRB_METHOD_PROC_P(m) (!MRB_METHOD_FUNC_P(m))
183#define MRB_METHOD_PROC(m) ((m).as.proc)
184#define MRB_METHOD_UNDEF_P(m) ((m).as.proc==NULL)
185#define MRB_METHOD_VISIBILITY(m) ((m).flags & MRB_METHOD_VISIBILITY_MASK)
186#define MRB_SET_VISIBILITY_FLAGS(f,v) ((f)=(((f)&~MRB_METHOD_VISIBILITY_MASK)|(v)))
187#define MRB_METHOD_SET_VISIBILITY(m,v) MRB_SET_VISIBILITY_FLAGS((m).flags,(v))
188
189#define MRB_METHOD_CFUNC_P(m) (MRB_METHOD_FUNC_P(m) || (MRB_METHOD_PROC(m)?(MRB_PROC_CFUNC_P(MRB_METHOD_PROC(m))):FALSE))
190/* use MRB_METHOD_CFUNC(m) only when MRB_METHOD_CFUNC_P(m) is true */
191#define MRB_METHOD_CFUNC(m) (MRB_METHOD_FUNC_P(m)?MRB_METHOD_FUNC(m):MRB_PROC_CFUNC(MRB_METHOD_PROC(m)))
192
193MRB_API mrb_value mrb_load_proc(mrb_state *mrb, const struct RProc *proc);
194
229
230void mrb_vm_ci_proc_set(mrb_callinfo *ci, const struct RProc *p);
231struct RClass * mrb_vm_ci_target_class(const mrb_callinfo *ci);
232void mrb_vm_ci_target_class_set(mrb_callinfo *ci, struct RClass *tc);
233struct REnv * mrb_vm_ci_env(const mrb_callinfo *ci);
234void mrb_vm_ci_env_set(mrb_callinfo *ci, struct REnv *e);
235
237
238#endif /* MRUBY_PROC_H */
Specifies the number of arguments a function takes.
mruby Symbol.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:108
mrb_irep structure
mrb_value(* mrb_func_t)(mrb_state *mrb, mrb_value self)
Function pointer type for a function callable by mruby.
Definition mruby.h:227
void mrb_vm_ci_env_clear(mrb_state *mrb, mrb_callinfo *ci)
It can be used to isolate top-level scopes referenced by blocks generated by mrb_load_string_cxt() or...
Definition vm.c:354
String class
Class class.
Definition class.h:17
Proc class.
Definition proc.h:26
Definition proc.h:53
Definition mruby.h:165
Definition mruby.h:196
Definition irep.h:55
Definition mruby.h:280
Definition boxing_nan.h:40